﻿using HardcoreGame.Common.Helpers;
using HardcoreGame.Common.Systems;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameContent.Bestiary;
using Terraria.GameContent.ItemDropRules;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Seasonsa.Summers
{
    public class FlyingBigTree : ModNPC
    {
        private Vector2 flightDirection;
        private float rotationSpeed;
        private int spawnSide = -1;

        public override void SetDefaults()
        {
            NPC.damage = Main.hardMode ? 10000 : 4000;
            NPC.defense = 0;
            NPC.lifeMax = 400;
            NPC.life = NPC.lifeMax;
            NPC.friendly = false;
            NPC.knockBackResist = 0f;
            NPC.aiStyle = -1;
            NPC.noTileCollide = true;
            NPC.noGravity = true;
            NPC.width = 668;
            NPC.height = 1558;
            NPC.lavaImmune = true;
            NPC.HitSound = null;
            NPC.DeathSound = null;
            NPC.gfxOffY = 16f;
        }

        public override void OnSpawn(IEntitySource source)
        {
            if (spawnSide == 0)
            {
                flightDirection = new Vector2(1f, Main.rand.NextFloat(-0.3f, 0.3f)).SafeNormalize(Vector2.UnitX);
            }
            else if (spawnSide == 1)
            {
                flightDirection = new Vector2(-1f, Main.rand.NextFloat(-0.3f, 0.3f)).SafeNormalize(Vector2.UnitX);
            }
            else
            {
                float windInfluence = MathHelper.Clamp(Main.windSpeedCurrent, -1f, 1f);
                flightDirection = new Vector2(windInfluence, Main.rand.NextFloat(-0.3f, 0.3f)).SafeNormalize(Vector2.UnitX);
            }

            NPC.velocity = flightDirection * Main.rand.Next(5, 10);
            rotationSpeed = MathHelper.ToRadians(Main.rand.Next(3, 10)) * (Main.rand.NextBool() ? 1 : -1);
        }

        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange(
            [
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Events.Rain,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.Bestiary.FlyingTree")
            ]);
        }

        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (EventSystem.activeEvents.Contains(5) && spawnInfo.Player.ZoneForest)
            {
                return 0.009f;

            }
            return 0f;
        }

        public override void AI()
        {
            float windForce = Main.windSpeedCurrent * 0.5f;
            if (spawnSide == 0)
            {
                flightDirection.X = MathHelper.Clamp(flightDirection.X + windForce * 0.01f, 0.5f, 1.5f);
            }
            else if (spawnSide == 1)
            {
                flightDirection.X = MathHelper.Clamp(flightDirection.X + windForce * 0.01f, -1.5f, -0.5f);
            }

            float baseSpeed = Main.hardMode ? 8f : 5f;
            NPC.velocity = flightDirection * baseSpeed * (1 + Math.Abs(windForce));
            NPC.rotation += rotationSpeed;

            if (Math.Abs(NPC.rotation) > MathHelper.TwoPi)
            {
                NPC.rotation %= MathHelper.TwoPi;
            }
            if (Main.GlobalTimeWrappedHourly % 1U == 0U && !Main.dedServ)
            {
                Vector2 leafSpawnPos = NPC.Center + new Vector2(0, -NPC.height / 2).RotatedBy(NPC.rotation);
                Gore.NewGore(
                    NPC.GetSource_FromAI(),
                    leafSpawnPos,
                    NPC.velocity * 0.3f + Main.rand.NextVector2Circular(1f, 1f),
                    GoreID.TreeLeaf_Normal,
                    Main.rand.NextFloat(0.5f, 1f)
                );
            }
            if (NPC.position.X < Main.screenPosition.X - NPC.width * 2
                || NPC.position.X > Main.screenPosition.X + Main.screenWidth + NPC.width * 2)
            {
                NPC.active = false;
            }
        }

        public override void HitEffect(NPC.HitInfo hit)
        {
            if (NPC.life <= 0)
            {
                for (int i = 0; i < 25; i++)
                {
                    Gore.NewGore(
                        NPC.GetSource_Death(),
                        NPC.getRect().GetRandomPoint(),
                        NPC.velocity * 0.5f,
                        GoreID.TreeLeaf_Normal,
                        Main.rand.NextFloat(1f, 2f)
                    );
                }
            }
            else
            {
                flightDirection = (flightDirection + new Vector2(hit.HitDirection * 0.2f, -0.1f)).SafeNormalize(Vector2.UnitX);
                if (spawnSide == 0 && flightDirection.X < 0) flightDirection.X = -flightDirection.X;
                if (spawnSide == 1 && flightDirection.X > 0) flightDirection.X = -flightDirection.X;
            }
        }

        public override void ModifyNPCLoot(NPCLoot npcLoot)
        {
            npcLoot.Add(ItemDropRule.Common(ItemID.Wood, 1, 10, 20));
            npcLoot.Add(ItemDropRule.Common(ItemID.Acorn, 1));
        }
    }
}
